<?php

defined('SYSPATH') or die('No direct access allowed.');

class Controller_Building extends Controller_Site {

    public function action_index() {
        if (Session::instance()->get('town_id') == NULL) {
            $town_id = ORM::factory('usertown')->where('user_id', '=', SESSION::instance()->get('user_id'))->where('hq', '=', 1)->find()->town_id;
            Session::instance()->set('town_id', $town_id);
        } else {
            $town_id = SESSION::instance()->get('town_id');
        }
        $places = ORM::factory('townbuildings')->where('town_id', '=', $town_id)->find_all();
        $this->setVar('places', $places);
    }

    public function action_show() {
        $param = explode('_', $this->request->param('id'));
        Session::instance()->set('town_id', $param[0]);
        Session::instance()->set('place_id', $param[1]);

        $building = ORM::factory('townbuildings')->where('town_id', '=', $param[0])->where('place_id', '=', $param[1])->find();
        $building_id = $building->building_id;
        $building = ORM::factory('buildings', $building_id);

        $storage = FALSE;
        $blank = FALSE;
        $barrack = FALSE;

        if ($building->speciality == 'barrack') {
            $barrack = TRUE;
            $units = ORM::factory('units')->find_all();
            $this->setVar('units', $units);
        }

        if ($building->speciality == 'storage') {
            $storage = TRUE;
            $products = ORM::factory('products')->find_all();
            foreach ($products as $key => $product) {
                $storaged_goods[$key]['prod_name'] = $product->name;
                $storaged_goods[$key]['quantity'] = ORM::factory('userresource')->where('user_id', '=', SESSION::instance()->get('user_id'))->where('town_id', '=', SESSION::instance()->get('town_id'))->where('product_id', '=', $product->id)->find()->quantity;
            }
            $this->setVar('storaged_goods', $storaged_goods);
        }

        if ($building->speciality == 'blank') {
            $blank = TRUE;
            $buildings = ORM::factory('buildings')->where('id', '!=', 'blank')->where('level', '=', 1)->find_all();
            $key = 0;
            foreach ($buildings as $building) {
                $req = ORM::factory('buildingreq')->where('building_id', '=', $building->id)->find_all();
                if ($req->count() == 0) {
                    $avaible[$key] = $building->id;
                    $key++;
                }
            }
            $this->setVar('avaible', $avaible);
        }

        $production = ORM::factory('buildingproduce')->where('building_id', '=', $building_id)->find_all();
        if ($production->count() == 0) {
            $production = NULL;
        }

        $build_cost = ORM::factory('buildcost')->where('building_id', '=', $building->next_level)->find_all();
        if ($build_cost->count() == 0) {
            $build_cost = NULL;
        }

        $build = ORM::factory('build')->where('user_id', '=', Session::instance()->get('user_id'))->where('town_id', '=', Session::instance()->get('town_id'))->where('place_id', '=', Session::instance()->get('place_id'))->find();

        $this->setVar('building', $building);
        $this->setVar('production', $production);
        $this->setVar('build_cost', $build_cost);
        $this->setVar('build', $build);
        $this->setVar('storage', $storage);
        $this->setVar('barrack', $barrack);
        $this->setVar('blank', $blank);
    }

    public function action_build() {
        $param = explode('_', $this->request->param('id'));
        $build = ORM::factory('build');
        $build->user_id = Session::instance()->get('user_id');
        $build->town_id = $param[0];
        $build->place_id = $param[1];
        $time = time();
        $build_time = ORM::factory('buildings', $param[2])->build_time;
        $build->start_time = $time;
        $build->finish_time = $time + $build_time;
        $build->wanted_building = $param[2];
        $build->save();
        $this->remove_resources($param[2]);
        $this->request->redirect('building/show/' . $param[0] . '_' . $param[1]);
    }

    public function action_finishbuild() {
        $param = explode('_', $this->request->param('id'));
        $build = ORM::factory('build')->where('user_id', '=', Session::instance()->get('user_id'))->where('town_id', '=', $param[0])->where('place_id', '=', $param[1])->find();
        $this->update_place($param[0], $param[1], $build->wanted_building);
        $build->delete();
    }

    public function action_cancelbuild() {
        $param = explode('_', $this->request->param('id'));
        $build = ORM::factory('build')->where('user_id', '=', Session::instance()->get('user_id'))->where('town_id', '=', $param[0])->where('place_id', '=', $param[1])->find();
        $build->delete();
        $this->add_back_resources($param[2]);
        $this->request->redirect('building/show/' . $param[0] . '_' . $param[1]);
    }

    public function remove_resources($building_id) {
        $cost = ORM::factory('buildcost')->where('building_id', '=', $building_id)->find_all();
        foreach ($cost as $c) {
            $user = ORM::factory('userresource')->where('user_id', '=', SESSION::instance()->get('user_id'))->where('town_id', '=', SESSION::instance()->get('town_id'))->where('product_id', '=', $c->product_id)->find();
            $user->quantity -= $c->quantity;
            $user->save();
        }
    }

    public function add_back_resources($building_id) {
        $cost = ORM::factory('buildcost')->where('building_id', '=', $building_id)->find_all();
        foreach ($cost as $c) {
            $user = ORM::factory('userresource')->where('user_id', '=', SESSION::instance()->get('user_id'))->where('town_id', '=', SESSION::instance()->get('town_id'))->where('product_id', '=', $c->product_id)->find();
            $user->quantity += $c->quantity;
            $user->save();
        }
    }

    public function update_place($town_id, $place_id, $new_building) {
        $building = ORM::factory('townbuildings')->where('town_id', '=', $town_id)->where('place_id', '=', $place_id)->find();
        $building->building_id = $new_building;
        $building->save();
    }

    public function action_destroy() {
        $param = explode('_', $this->request->param('id'));
        $building = ORM::factory('townbuildings')->where('town_id', '=', $param[0])->where('place_id', '=', $param[1])->find();
        $building->building_id = 1;
        $building->save();
        $this->request->redirect('building/show/' . $param[0] . '_' . $param[1]);
    }

}